package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"test/controllers"
	"test/middlewares"
	"test/system"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
)

func SetupRouter(r *gin.Engine) *gin.Engine {

	/*r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "Welcome GO GIN",
		})
	})*/
	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong")
	})

	// 首页
	/*r.GET("/", func(c *gin.Context) {
		user, _ := c.Get(controllers.CONTEXT_USER_KEY)
		c.HTML(200, "index/welcome.html", gin.H{
			"user": user,
		})
	})*/

	// 博文
	r.GET("/", controllers.IndexGet)//test
	r.GET("/index", controllers.IndexGet)
	r.GET("/post/:id", controllers.PostGet)

	r.GET("/test", controllers.IndexTest)
	r.GET("/json", controllers.IndexJson)
	r.GET("/home", controllers.Home)
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{
			"status": 1,
			//"dat": []string,
			"msg": "error",
		})
	})
	r.GET("/tag/:tag", controllers.TagGet)


	// 用户登入退出
	r.GET("/signin", func(c *gin.Context) {
		c.HTML(200, "auth/signin.html", nil)
	})
	r.POST("/signin", controllers.SignInPost)
	r.GET("/logout", func(c *gin.Context) {
		session := sessions.Default(c)
		session.Clear()
		session.Save()
		c.Redirect(http.StatusSeeOther, "/signin")
	})
	r.GET("/auth/:authType", controllers.AuthGet)

	// captcha
	r.GET("/captcha", controllers.CaptchaGet)


	// 注册
	if system.GetConfiguration().SignupEnabled {
		r.GET("/signup", func(c *gin.Context) {
			c.HTML(200, "auth/signup.html", nil)
		})
		r.POST("/signup", controllers.SignupPost)
	}

	visitor := r.Group("/visitor")
	visitor.Use(middlewares.AdminScopeRequired())
	{
		//评论
		visitor.POST("/new_comment", controllers.CommentPost)
		visitor.POST("/comment/:id/delete", controllers.CommentDelete)
	}

	//其他页面
	r.GET("/page/:id", controllers.PageGet)


	// 后台
	auth := r.Group("/admin")
	auth.Use(middlewares.AdminScopeRequired())
	{
		// 首页
		auth.GET("/index", controllers.AdminIndex)

		// 博文
		auth.GET("/post", controllers.PostIndex)
		auth.GET("/new_post", func(c *gin.Context) {
			user, _ := c.Get(controllers.CONTEXT_USER_KEY)
			c.HTML(http.StatusOK, "post/new.html", gin.H{
				"user": user,
			})
		})
		auth.POST("/new_post", controllers.PostCreate)
		auth.GET("/post/:id/edit", controllers.PostEdit)
		auth.POST("/post/:id/edit", controllers.PostUpdate)
		auth.POST("/post/:id/delete", controllers.PostDelete)

		// tag
		auth.POST("/new_tag", controllers.TagCreate)
		//
		auth.GET("/user", controllers.UserIndex)
		//auth.POST("/user/:id/lock", controllers.UserLock)
		// profile
		auth.GET("/profile", controllers.ProfileGet)

		// page
		auth.GET("/page", controllers.PageIndex)
		auth.GET("/new_page", controllers.PageNew)
		auth.POST("/new_page", controllers.PageCreate)
		auth.GET("/page/:id/edit", controllers.PageEdit)
		auth.POST("/page/:id/edit", controllers.PageUpdate)
		auth.POST("/page/:id/delete", controllers.PageDelete)

	}

	/*r.GET("/test", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello world")
	})
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})

	})*/

	r.POST("/form", func(c *gin.Context) {
		type1 := c.DefaultPostForm("type", "alert")
		username := c.PostForm("username")
		pwd := c.PostForm("password")
		c.String(http.StatusOK, fmt.Sprintf("type is %s, username is %s, password is %s", type1, username, pwd))
	})
	return  r
}



//setSessions initializes sessions & csrf middlewares
func SetSessions(router *gin.Engine) {
	config := system.GetConfiguration()
	store := cookie.NewStore([]byte(config.SessionSecret))
	store.Options(sessions.Options{HttpOnly: true, MaxAge: 7 * 86400, Path: "/"}) //Also set Secure: true if using SSL, you should though
	router.Use(sessions.Sessions("gin-session", store))

}
